package com.atguigu.service.Impl;

import com.atguigu.dao.BaseDao;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.util.CastUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-08 23:19
 * @description：
 */
@DubboService
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {
    @Autowired
    private CommunityDao communityDao;
    @Autowired
    private DictDao dictDao;

    @Override
    public BaseDao<Community> getBaseDao() {
        return communityDao;
    }

    @Override
    public PageInfo<Community> findPage(Map<String, Object> filters) {
        int pageNum = CastUtil.castInt(filters.get("pageNum"), 1);
        int pageSize = CastUtil.castInt(filters.get("pageSize"), 3);
        PageHelper.startPage(pageNum, pageSize);
        List<Community> page = getBaseDao().findPage(filters);
        for (Community community : page) {
            Dict areaId = dictDao.findById(community.getAreaId());
            community.setAreaName(areaId.getName());
            Dict plateId = dictDao.findById(community.getPlateId());
            community.setPlateName(plateId.getName());
        }
        return new PageInfo<Community>(page, 3);
    }

    @Override
    public List<Community> findAll() {
        return communityDao.findAll();
    }

    @Override
    public Community findById(Serializable id) {
        Community community = communityDao.findById(id);
        community.setAreaName(dictDao.findById(community.getAreaId()).getName());
        community.setPlateName(dictDao.findById(community.getPlateId()).getName());
        return community;
    }
}
